package com.longtop.leaning.rms;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;

import com.longtop.leaning.entity.VFlt;
import com.longtop.leaning.serializable.FltSerializable;
import com.longtop.leaning.util.StringUtils;

public class FltDao {

	public void test() {
		try {
			RecordStore rs = RMSUtil.getFltRecordStore();

			VFlt flt = new VFlt();
			flt.setId(new Long(rs.getNextRecordID()));
			flt.setFltNum("测FM"+rs.getNextRecordID()+"-中国");
			flt.setFltDate("");

			addFlt(flt);

			System.out.println(rs.getSize());
			System.out.println(rs.getNumRecords());

			RecordEnumeration re = rs.enumerateRecords(new FltFilter("", "", -1), new FltRecordComparator(), false);

			while (re.hasNextElement()) {
				byte nextRec[] = re.nextRecord();
				System.out.println("read record bytes : " + nextRec);
				flt = (VFlt) FltSerializable.getInstance().deserialize(nextRec);
				System.out.println("主键ID：" + flt.getId());
				System.out.println(", 航班号：" + flt.getFltNum());
			}

			Vector flts = findFlts("3", "", -1);
			System.out.println("flts size: " + flts.size());
			//addFlts(flts);
			//System.out.println("after add flts: "+rs.getNumRecords());
			RMSUtil.closeRecordStore(rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int addFlt(VFlt flt) {
		RecordStore rs = RMSUtil.getFltRecordStore();
		int recordId = RMSUtil.addRecord(rs, FltSerializable.getInstance().serialize(flt));
		RMSUtil.closeRecordStore(rs);
		return recordId;
	}
	
	public void addFlts(Vector flts) {
		RecordStore rs = RMSUtil.getFltRecordStore();
		
		Enumeration fltE = flts.elements();
		while(fltE.hasMoreElements()){
			VFlt flt = (VFlt) fltE.nextElement();
			RMSUtil.addRecord(rs, FltSerializable.getInstance().serialize(flt));
		}
		
		RMSUtil.closeRecordStore(rs);
	}

	public Vector findFlts(String fltNum, String fltdate, int fltstatus) {
		RecordStore rs = RMSUtil.getFltRecordStore();

		RecordEnumeration re = RMSUtil.enumerateRecords(rs, new FltFilter(fltNum, fltdate, fltstatus),
				new FltRecordComparator(), false);
		Vector flts = new Vector();
		while (re.hasNextElement()) {
			byte nextRec[] = RMSUtil.nextRecord(re);
			VFlt flt = (VFlt) FltSerializable.getInstance().deserialize(nextRec);
			flts.addElement(flt);
		}

		RMSUtil.closeRecordStore(rs);
		return flts;
	}
	
	public void clearFlt() {
		RMSUtil.deleteRecordStore(RMSUtil.FLT_RMS_NAME);
	}

	class FltFilter implements RecordFilter {
		private String fltNum = ""; // 航班号
		private String fltdate = null;// 计划起飞日期及时间
		private int fltstatus = -1;// 航班起飞状态 0 正常 1 延误 2 取消

		public FltFilter(String fltNum, String fltdate, int fltstatus) {
			this.fltNum = fltNum;
			this.fltdate = fltdate;
			this.fltstatus = fltstatus;
		}

		public boolean matches(byte[] candidate) {
			VFlt flt = (VFlt) FltSerializable.getInstance().deserialize(candidate);

			if (!StringUtils.isEmpty(fltNum)) {
				return flt.getFltNum().indexOf(fltNum.trim()) != -1;
			}

			if (fltdate != null) {
				return flt.getFltDate().indexOf(fltdate.trim()) != -1;
			}

			if (fltstatus >= 0) {
				return flt.getFltStatus().equals(String.valueOf(fltstatus));
			}

			return true;
		}
	}

	class FltRecordComparator implements RecordComparator {
		public int compare(byte[] rec1, byte[] rec2) {
			VFlt flt1 = (VFlt) FltSerializable.getInstance().deserialize(rec1);
			VFlt flt2 = (VFlt) FltSerializable.getInstance().deserialize(rec2);

			if (flt1.getId().longValue() < flt2.getId().longValue()) {
				return RecordComparator.PRECEDES;
			} else if (flt1.getId().longValue() > flt2.getId().longValue()) {
				return RecordComparator.FOLLOWS;
			}
			return RecordComparator.EQUIVALENT;
		}
	}

}
